using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Runtime.Serialization;
using System.Collections;
using System.Data;
using System.ComponentModel;
using System.Data.OracleClient;
using WPFExcelReport.Helpers;
using WPFExcelReport.Controls;
namespace WPFExcelReport
{
    public partial class VDRBKOPF
    {
        public DataRow Convert2DRBKOPF(DataRow dwInput, string ITXDBVERSION, bool IsConvert = false)
        {
            DataSet SResult = new DataSet();
            SResult = QueryDRBKOPF(dwInput, 0);
            DataRow dwReturn = null;
            if (SResult.Tables[0].Rows.Count > 0)
            {
                dwReturn = SResult.Tables[0].Rows[0];
            }
            else
            {
                dwReturn = SResult.Tables[0].NewRow();
            }
            switch (ITXDBVERSION)
            {
                case "YGR":
                    #region=========
         dwReturn["DRBKMANDANT"] = " ".PadRight(2 ); 
         dwReturn["DRBKLFDNR"] = " ".PadRight(4 ); 
         dwReturn["DRBKKURZBEZ"] = " ".PadRight(10 ); 
         dwReturn["DRBKBEZ1"] = " ".PadRight(50 ); 
         dwReturn["DRBKBEZ2"] = " ".PadRight(50 ); 
         dwReturn["DRBKSUBSYS"] = " ".PadRight(8 ); 
         dwReturn["DRBKSTARTD"] = " ".PadRight(0 ); 
         dwReturn["DRBKSTARTZ"] = " ".PadRight(0 ); 
         dwReturn["DRBKLSTARTD"] = " ".PadRight(0 ); 
         dwReturn["DRBKLSTARTZ"] = " ".PadRight(0 ); 
         dwReturn["DRBKLENDED"] = " ".PadRight(0 ); 
         dwReturn["DRBKLENDEZ"] = " ".PadRight(0 ); 
         dwReturn["DRBKPARMD"] = " ".PadRight(20 ); 
         dwReturn["DRBKPARMZ"] = " ".PadRight(20 ); 
         dwReturn["DRBKSTATUS"] = " ".PadRight(1 ); 
         dwReturn["DRBKMNR"] = " ".PadRight(4 ); 
         dwReturn["DRBKMPAR1"] = " ".PadRight(40 ); 
         dwReturn["DRBKMPAR2"] = " ".PadRight(20 ); 
         dwReturn["DRBKMPAR3"] = " ".PadRight(80 ); 
         dwReturn["DRBKNEUDAT"] = " ".PadRight(0 ); 
         dwReturn["DRBKNEUBEN"] = " ".PadRight(8 ); 
         dwReturn["DRBKAENDAT"] = " ".PadRight(0 ); 
         dwReturn["DRBKAENBEN"] = " ".PadRight(8 ); 
         dwReturn["DRBKAENDZL"] = " ".PadRight(0 ); 
         dwReturn["DRBKNEUSTART"] = " ".PadRight(1 ); 
         dwReturn["DRBKEXKLUSIV"] = " ".PadRight(1 ); 
         dwReturn["DRBKPID"] = " ".PadRight(9 ); 
         dwReturn["DRBKFILLER"] = " ".PadRight(30 ); 
                    #endregion
                    break;
                case "JLT":

                    break;
                default:

                    break;
            }
            if (IsConvert)
            {
                foreach (DataColumn cm in dwInput.Table.Columns)
                {
                    if (dwInput[cm.ColumnName].ToString() == "")
                        continue;
                    if (dwReturn.Table.Columns.Contains(cm.ColumnName))
                    {
                        dwReturn[cm.ColumnName] = dwInput[cm.ColumnName];
                    }
                }
            }
            return dwReturn;
        }
        public void MapDRBKOPF(ref DataRow dwLocal, DataRow dwData)
        {
            BlindMap(ref dwLocal, dwData);
        }
        public void SaveDRBKOPF(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            DataSet setResult = new DataSet();
            DataTable tbLocal = null;
            DataRow dwLocal = null;
            bool flgExist = false;
            setResult = QueryDRBKOPF(dwData);
            if (setResult.Tables.Count > 0)
            {
                tbLocal = setResult.Tables[0];
                if (tbLocal.Rows.Count > 0)
                {
                    flgExist = true;
                    dwLocal = tbLocal.Rows[0];
                }
                else
                {
                    dwLocal = tbLocal.NewRow();
                }
            }
            MapDRBKOPF(ref dwLocal, dwData);

            if (!flgExist)
            {

                InsertDRBKOPF(dwLocal, ITXDBVERSION);
            }
            UpdateDRBKOPF(dwLocal);
        }
        public void InsertDRBKOPF(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            OracleCommand or1;
            or1 = OracleDB.CreateCommand();
            switch (ITXDBVERSION)
            {
                case "YGR":
                    or1.CommandText = "INSERT INTO DRBKOPF(VAKBMANDANT, VAKBKOLL, VAKBART, VAKBKUNDNR, VAKBFARBE, VAKBKDARTNR, VAKBKDARTBEZ, VAKBKDFARBNR, VAKBANDRUCK, VAKBMEGEW, VAKBMENGEH, VAKBGEWVON, VAKBGEWBIS, VAKBGEWCA, VAKBGEWFKT, VAKBGEWGRP, VAKBMEGFKT, VAKBMEGGRP, VAKBAENDZL, VAKBAENDBEN, VAKBAENDDAT, VAKBNEUBEN, VAKBNEUDAT, VAKBFILLER)"
                        + " VALUES (:VAKBMANDANT, :VAKBKOLL, :VAKBART, :VAKBKUNDNR, :VAKBFARBE, '                         ', '                         ', '                             ', ' ', '   ', '   ', 0, 0, ' ', '000000000000', ' ', '000000000000', ' ', 0, '        ', '000000', '        ', '      ', '                                          ')";
                    or1.Parameters.Add("VAKBMANDANT", OracleType.NVarChar).Value = dwData["VAKBMANDANT"].ToString();
                    or1.Parameters.Add("VAKBKOLL", OracleType.NVarChar).Value = dwData["VAKBKOLL"].ToString();
                    or1.Parameters.Add("VAKBART", OracleType.NVarChar).Value = dwData["VAKBART"].ToString();
                    or1.Parameters.Add("VAKBFARBE", OracleType.NVarChar).Value = dwData["VAKBFARBE"].ToString();
                    or1.Parameters.Add("VAKBKUNDNR", OracleType.NVarChar).Value = dwData["VAKBKUNDNR"].ToString();
                    break;
                //VAFMANDANT VAFKOLL VAFART VAFFARBE VAFBEZ VAFFFARBE

            }
            
            Connect2Proxy(or1);
        }
        public DataSet QueryDRBKOPF(DataRow dwData, int option = 1)
        {
            DataSet Return;
            DataSet Result = new DataSet();
            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 0:
                    or1.CommandText = "SELECT * FROM DRBKOPF WHERE  ROWNUM<2 ";
                    break;
              
                case 99:
                    or1.CommandText = "SELECT * FROM DRBKOPF  WHERE ROWNUM<50";
                    break;
                default:
                    break;
            }
            Result = Connect2Proxy(or1);
            if (Result.Tables.Count > 0)
                Result.Tables[0].TableName = "DRBKOPF";
            Return = Result;
            return Return;
        }
        public void DeleteDRBKOPF(DataRow dwData, int option = 1)
        {

            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    or1.CommandText = "DELETE FROM DRBKOPF   WHERE  VAKBMANDANT=:VAKBMANDANT AND VAKBKOLL=:VAKBKOLL AND VAKBART =:VAKBART AND VAKBFARBE=:VAKBFARBE AND VAKBKUNDNR=:VAKBKUNDNR";
                     or1.Parameters.Add("VAKBMANDANT", OracleType.NVarChar).Value = dwData["VAKBMANDANT"].ToString();
                    or1.Parameters.Add("VAKBKOLL", OracleType.NVarChar).Value = dwData["VAKBKOLL"].ToString();
                    or1.Parameters.Add("VAKBART", OracleType.NVarChar).Value = dwData["VAKBART"].ToString();
                    or1.Parameters.Add("VAKBFARBE", OracleType.NVarChar).Value = dwData["VAKBFARBE"].ToString();
                    or1.Parameters.Add("VAKBKUNDNR", OracleType.NVarChar).Value = dwData["VAKBKUNDNR"].ToString();
                    break;
                default:
                    break;
            }
            Connect2Proxy(or1);
        } 
      
        public void UpdateDRBKOPF(DataRow dwData, string ITXDBVERSION = "50.24", int option = 1)
        {
            OracleCommand or1;
            DataSet Result = new DataSet();
            or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    #region====Command==== 
                    or1.CommandText = "UPDATE DRBKOPF SET "
                    +"DRBKMANDANT= :DRBKMANDANT,"
                    +"DRBKLFDNR= :DRBKLFDNR,"
                    +"DRBKKURZBEZ= :DRBKKURZBEZ,"
                    +"DRBKBEZ1= :DRBKBEZ1,"
                    +"DRBKBEZ2= :DRBKBEZ2,"
                    +"DRBKSUBSYS= :DRBKSUBSYS,"
                    +"DRBKSTARTD= :DRBKSTARTD,"
                    +"DRBKSTARTZ= :DRBKSTARTZ,"
                    +"DRBKLSTARTD= :DRBKLSTARTD,"
                    +"DRBKLSTARTZ= :DRBKLSTARTZ,"
                    +"DRBKLENDED= :DRBKLENDED,"
                    +"DRBKLENDEZ= :DRBKLENDEZ,"
                    +"DRBKPARMD= :DRBKPARMD,"
                    +"DRBKPARMZ= :DRBKPARMZ,"
                    +"DRBKSTATUS= :DRBKSTATUS,"
                    +"DRBKMNR= :DRBKMNR,"
                    +"DRBKMPAR1= :DRBKMPAR1,"
                    +"DRBKMPAR2= :DRBKMPAR2,"
                    +"DRBKMPAR3= :DRBKMPAR3,"
                    +"DRBKNEUDAT= :DRBKNEUDAT,"
                    +"DRBKNEUBEN= :DRBKNEUBEN,"
                    +"DRBKAENDAT= :DRBKAENDAT,"
                    +"DRBKAENBEN= :DRBKAENBEN,"
                    +"DRBKAENDZL= :DRBKAENDZL,"
                    +"DRBKNEUSTART= :DRBKNEUSTART,"
                    +"DRBKEXKLUSIV= :DRBKEXKLUSIV,"
                    +"DRBKPID= :DRBKPID,"
                    +"DRBKFILLER= :DRBKFILLER,"
                        + " WHERE  "
                        + "  VAKBMANDANT=:VAKBMANDANT AND VAKBKOLL=:VAKBKOLL AND VAKBART =:VAKBART AND VAKBFARBE=:VAKBFARBE AND VAKBKUNDNR=:VAKBKUNDNR";
                    #endregion

                    #region====Parameters====
          or1.Parameters.Add("DRBKMANDANT", OracleType.NVarChar).Value = dwData["DRBKMANDANT"].ToString(); 
          or1.Parameters.Add("DRBKLFDNR", OracleType.NVarChar).Value = dwData["DRBKLFDNR"].ToString(); 
          or1.Parameters.Add("DRBKKURZBEZ", OracleType.NVarChar).Value = dwData["DRBKKURZBEZ"].ToString(); 
          or1.Parameters.Add("DRBKBEZ1", OracleType.NVarChar).Value = dwData["DRBKBEZ1"].ToString(); 
          or1.Parameters.Add("DRBKBEZ2", OracleType.NVarChar).Value = dwData["DRBKBEZ2"].ToString(); 
          or1.Parameters.Add("DRBKSUBSYS", OracleType.NVarChar).Value = dwData["DRBKSUBSYS"].ToString(); 
          or1.Parameters.Add("DRBKSTARTD", OracleType.NVarChar).Value = dwData["DRBKSTARTD"].ToString(); 
          or1.Parameters.Add("DRBKSTARTZ", OracleType.NVarChar).Value = dwData["DRBKSTARTZ"].ToString(); 
          or1.Parameters.Add("DRBKLSTARTD", OracleType.NVarChar).Value = dwData["DRBKLSTARTD"].ToString(); 
          or1.Parameters.Add("DRBKLSTARTZ", OracleType.NVarChar).Value = dwData["DRBKLSTARTZ"].ToString(); 
          or1.Parameters.Add("DRBKLENDED", OracleType.NVarChar).Value = dwData["DRBKLENDED"].ToString(); 
          or1.Parameters.Add("DRBKLENDEZ", OracleType.NVarChar).Value = dwData["DRBKLENDEZ"].ToString(); 
          or1.Parameters.Add("DRBKPARMD", OracleType.NVarChar).Value = dwData["DRBKPARMD"].ToString(); 
          or1.Parameters.Add("DRBKPARMZ", OracleType.NVarChar).Value = dwData["DRBKPARMZ"].ToString(); 
          or1.Parameters.Add("DRBKSTATUS", OracleType.NVarChar).Value = dwData["DRBKSTATUS"].ToString(); 
          or1.Parameters.Add("DRBKMNR", OracleType.NVarChar).Value = dwData["DRBKMNR"].ToString(); 
          or1.Parameters.Add("DRBKMPAR1", OracleType.NVarChar).Value = dwData["DRBKMPAR1"].ToString(); 
          or1.Parameters.Add("DRBKMPAR2", OracleType.NVarChar).Value = dwData["DRBKMPAR2"].ToString(); 
          or1.Parameters.Add("DRBKMPAR3", OracleType.NVarChar).Value = dwData["DRBKMPAR3"].ToString(); 
          or1.Parameters.Add("DRBKNEUDAT", OracleType.NVarChar).Value = dwData["DRBKNEUDAT"].ToString(); 
          or1.Parameters.Add("DRBKNEUBEN", OracleType.NVarChar).Value = dwData["DRBKNEUBEN"].ToString(); 
          or1.Parameters.Add("DRBKAENDAT", OracleType.NVarChar).Value = dwData["DRBKAENDAT"].ToString(); 
          or1.Parameters.Add("DRBKAENBEN", OracleType.NVarChar).Value = dwData["DRBKAENBEN"].ToString(); 
          or1.Parameters.Add("DRBKAENDZL", OracleType.NVarChar).Value = dwData["DRBKAENDZL"].ToString(); 
          or1.Parameters.Add("DRBKNEUSTART", OracleType.NVarChar).Value = dwData["DRBKNEUSTART"].ToString(); 
          or1.Parameters.Add("DRBKEXKLUSIV", OracleType.NVarChar).Value = dwData["DRBKEXKLUSIV"].ToString(); 
          or1.Parameters.Add("DRBKPID", OracleType.NVarChar).Value = dwData["DRBKPID"].ToString(); 
          or1.Parameters.Add("DRBKFILLER", OracleType.NVarChar).Value = dwData["DRBKFILLER"].ToString(); 
                    #endregion
                    break; 
                default:
                    break;
            }


            Connect2Proxy(or1);
        }
    }
}
